package com.gnerv.plug.note.dialog;

import com.gnerv.plug.note.data.DataCenter;
import com.gnerv.plug.note.entity.NoteInfo;
import com.gnerv.plug.utils.UUIDUtils;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.HighlightableComponent;
import com.intellij.ui.components.panels.HorizontalBox;
import groovy.io.FileType;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CreateNoteDialog extends DialogWrapper {

    private String ukId;
    private EditorTextField noteTitle;
    private EditorTextField noteContent;

    public CreateNoteDialog() {
        super(true);
        setTitle("create note");
        init();
        this.ukId = UUIDUtils.getLower();
    }

    public CreateNoteDialog(NoteInfo noteInfo) {
        super(true);
        setTitle("create note");
        this.ukId = noteInfo.getUkId();
        setNoteTitle(noteInfo.getTitle());
        setNoteContent(noteInfo.getContent());
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel rootPanel = new JPanel(new BorderLayout());
        noteTitle = new EditorTextField("title");
        noteContent = new EditorTextField(""){
            @Override
            protected EditorEx createEditor() {
                EditorEx editor = super.createEditor();
                editor.setVerticalScrollbarVisible(true);
                editor.setHorizontalScrollbarVisible(true);
                editor.setOneLineMode(false);
                return editor;
            }
        };
        noteContent.setPreferredSize(new Dimension(500, 400));
        rootPanel.add(noteTitle, BorderLayout.NORTH);
        rootPanel.add(noteContent, BorderLayout.CENTER);
        return rootPanel;
    }

    @Override
    protected JComponent createSouthPanel() {
        JPanel rootPanel = new JPanel(new FlowLayout());
        JButton save = new JButton("save");
        save.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                NoteInfo noteInfo = new NoteInfo(ukId, noteTitle.getText(), noteContent.getText());
                DataCenter.NOTE_DATA.put(noteInfo.getUkId(), noteInfo);
                DataCenter.refreshToolWindowTableData("");
                close(1);
            }
        });
        JButton cancel = new JButton("cancel");
        cancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                close(1);
            }
        });
        rootPanel.add(save);
        rootPanel.add(cancel);
        return rootPanel;
    }

    public void setUkId(String ukId) {
        this.ukId = ukId;
    }

    public void setNoteTitle(String noteTitle) {
        this.noteTitle.setText(noteTitle);;
    }

    public void setNoteContent(String noteContent) {
        this.noteContent.setText(noteContent);
    }
}
